namespace AltNetUK.Models
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Web;

    [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Event", Justification = "This is the proper name.")]
    [XmlRoot(ElementName = "event")]
    public class Event : Resource
    {
        private Registrations _registrations;

        public Event()
            : base(true)
        {
        }

        public static Event NotFound(UtcDateTime date)
        {
            if (null == date)
            {
                throw new ArgumentNullException("date");
            }

            Event value = new Default().Object<Event>();
            value.Title = date.Date;
            value.Path = "/" + date;

            return value;
        }

        public static Event Calendar(UtcDateTime date)
        {
            if (null == date)
            {
                throw new ArgumentNullException("date");
            }

            Event result = null;

            ScheduleCollection schedule = new Default().Object<ScheduleCollection>();
            foreach (Event item in schedule)
            {
                if (string.Concat("/", date).Equals(item.Path, StringComparison.OrdinalIgnoreCase))
                {
                    result = item;
                    break;
                }
            }

            return result ?? Event.NotFound(date);
        }

        [XmlIgnore]
        public Registrations Registrations
        {
            get
            {
                if (null == this._registrations)
                {
                    this._registrations = new Registrations(this.Key);
                }

                return this._registrations;
            }
        }

        [XmlAttribute(AttributeName = "key")]
        public string Key
        {
            get;
            set;
        }

        [XmlAttribute(AttributeName = "name")]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute(AttributeName = "path")]
        public string Path
        {
            get;
            set;
        }

        [XmlElement(ElementName = "start")]
        public UtcDateTime Start
        {
            get;
            set;
        }

        [XmlElement(ElementName = "end")]
        public UtcDateTime End
        {
            get;
            set;
        }

        [XmlElement(ElementName = "address")]
        public Address Address
        {
            get;
            set;
        }

        [XmlElement(ElementName = "description")]
        public string Description
        {
            get;
            set;
        }

        [XmlElement(ElementName = "detail")]
        public string Detail
        {
            get;
            set;
        }
    }
}